package cc.vv.baselibrary.util;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import cc.vv.BaseNewApplication;
import cc.vv.baselibrary.bean.info.GeRenDataInfo;
import cc.vv.baselibrary.bean.info.UserInfoObj;
import cc.vv.baselibrary.global.UserInfoSharePreKey;
import cc.vv.lklibrary.log.LogOperate;

import static android.content.Context.TELEPHONY_SERVICE;

/**
 * 用户信息管理操作类
 * Created by sun.li on 2017/7/27.
 */

public class UserInfoManageUtil {
    /*用户账号类型:手机号 2；*/
    public static final String UserLoginType_PhoneCode = "2";

    /*用户账号类型:邮箱 1；第三方微信3*/
    public static final String UserLoginType_MailCode = "1";

    /*用户账号类型:第三方微信 3*/
    public static final String UserLoginType_WeChatCode = "3";


    /**
     * 获取用户ID
     */
    public static String getUserId() {
        return LKPrefUtil.getString(UserInfoSharePreKey.USER_ID, "");
    }

    /**
     * 是否登录
     */
    public static boolean isLogin() {
        return LKPrefUtil.getBoolean(UserInfoSharePreKey.LOGIN_INFO, false);
    }

    /**
     * 保存登录
     */
    public static void saveLogin(boolean isLogin) {
        LKPrefUtil.putBoolean(UserInfoSharePreKey.LOGIN_INFO, isLogin);
    }


    /**
     * 保存Token
     */
    public static void saveToken(String token) {
        LKPrefUtil.putString(UserInfoSharePreKey.TOKEN, token);
    }

    /**
     * 获取Token
     */
    public static String getToken() {
        String token;
        token = LKPrefUtil.getString(UserInfoSharePreKey.TOKEN, "");
        return token;
    }


    /**
     * 保存SessionId
     */
    public static void saveSessionId(String sessionId) {
        LKPrefUtil.putString(UserInfoSharePreKey.SESSIONID, sessionId);
    }

    /**
     * 获取SessionId
     */
    public static String getSessionId() {
        String token;
        token = LKPrefUtil.getString(UserInfoSharePreKey.SESSIONID, "");
        return token;
    }

    /**
     * 保存手机号
     */
    public static void savePhone(String telPhone) {
        LKPrefUtil.putString(UserInfoSharePreKey.USER_TEL_PHONE, telPhone);
    }

    /**
     * 获取手机号
     */
    public static String getPhone() {
        return LKPrefUtil.getString(UserInfoSharePreKey.USER_TEL_PHONE, "");
    }


    /**
     * 保存手机号
     */
    public static void savePassword(String passWord) {
        LKPrefUtil.putString(UserInfoSharePreKey.TEL_PASSWORD, passWord);
    }

    /**
     * 获取手机号
     */
    public static String getPassWord() {
        return LKPrefUtil.getString(UserInfoSharePreKey.TEL_PASSWORD, "");
    }


    /**
     * 获取用户信息
     *
     * @return
     */
    public static UserInfoObj getUserInfo() {
        UserInfoObj userInfoObj;
        try {
            userInfoObj = LKPrefUtil.getObject(UserInfoSharePreKey.USERINFO_DATA, new UserInfoObj());
        } catch (Exception e) {
            LogOperate.e(e.getMessage(), e);
            userInfoObj = null;
        }
        return userInfoObj;
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public static GeRenDataInfo getGeRenInfo() {
        GeRenDataInfo geRenDataInfo;
        try {
            geRenDataInfo = LKPrefUtil.getObject(UserInfoSharePreKey.GE_REN_DATA, new GeRenDataInfo());
        } catch (Exception e) {
            LogOperate.e(e.getMessage(), e);
            geRenDataInfo = null;
        }
        return geRenDataInfo;
    }


    /**
     * 存储用户信息数据
     *
     * @param userInfoObj 用户信息对象数据
     */
    public static void saveUserInfo(UserInfoObj userInfoObj) {
        if (null != userInfoObj) {
            LKPrefUtil.putObject(UserInfoSharePreKey.USERINFO_DATA, userInfoObj);
            LKPrefUtil.putString(UserInfoSharePreKey.TOKEN, userInfoObj.getToken());
            LKPrefUtil.putString(UserInfoSharePreKey.USER_ID, userInfoObj.getUserId());
            LKPrefUtil.putString(UserInfoSharePreKey.USER_NAME, userInfoObj.getUserName());
            LKPrefUtil.putString(UserInfoSharePreKey.USER_TEL_PHONE, userInfoObj.getUserAccount());
            LKPrefUtil.putString(UserInfoSharePreKey.SESSIONID, userInfoObj.getSESSIONID());
        }
    }


    public static boolean isEqualMobile(String mobile) {
        boolean isEqual;
        if (TextUtils.isEmpty(mobile)) {
            isEqual = false;
        } else {
            String saveTel = LKPrefUtil.getString(UserInfoSharePreKey.USER_TEL_PHONE, "");
            isEqual = mobile.equals(saveTel);
        }
        return isEqual;
    }


    /**
     * 获取设备唯一标识IMEI
     */
    @SuppressLint("MissingPermission")
    public static String getIMEI() {
        if (BaseNewApplication.getApplication() != null) {
            String szImei = "";
            try {
                TelephonyManager TelephonyMgr = (TelephonyManager) BaseNewApplication.getApplication().getSystemService
                        (TELEPHONY_SERVICE);
                if (ActivityCompat.checkSelfPermission(BaseNewApplication.getApplication(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    return "";
                }
                szImei = TelephonyMgr.getDeviceId();
            } catch (Exception e) {
                LogOperate.e(e.getMessage(), e);
            }
            if (!TextUtils.isEmpty(szImei)) {
                return MD5.md5(szImei);
            }
        }
        return "";
    }


    /**
     * 获取坂本明
     *
     * @return
     */
    public static String getVersionName(Context context) {
        if (context == null) {
            return "1.0.0";
        }
        String versionName = "1.0.0";
        try {
            PackageManager manager = context.getPackageManager();
            //第二个参数代表额外的信息，例如获取当前应用中的所有的Activity
            PackageInfo packageInfo = manager.getPackageInfo(getPackageName(context), PackageManager.GET_ACTIVITIES);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            LKLogUtil.e(e.getMessage(), e);
        }
        LKLogUtil.e("VersionName：" + versionName);
        return versionName;
    }

    /**
     * 获取应用包名
     *
     * @return
     */
    private static String getPackageName(Context context) {
        String packageName = "cc.vv.btpartyjob";
        if (context != null) {
            try {
                packageName = context.getApplicationContext().getPackageName();
            } catch (Exception e) {
                LKLogUtil.e(e.getMessage(), e);
                packageName = "cc.vv.btpartyjob";
            }
        }
        LKLogUtil.e("PackageName：" + packageName);
        return packageName;
    }

    public static boolean getUserIsLogin() {
        if (TextUtils.isEmpty(getUserId())) {
            return false;
        }
        return true;
    }


    /**
     * 获取版本号
     *
     * @return
     */
    public static int getVersionCode(Context context) {
        if (context == null) {
            return 0;
        }
        int code = 0;
        try {
            //获取包管理器
            PackageManager manager = context.getPackageManager();
            //通过当前的包名获取包的信息
            PackageInfo info = manager.getPackageInfo(getPackageName(context), 0);
            if (info != null) {
                code = info.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
            LKLogUtil.e(e.getMessage(), e);
        }
        return code;
    }


}
